const config = {
	logo: '@/static/logo.png',
	name: '企蝶',
	forms: []
}

const tagColor = ['h-tag-bg-blue', 'h-tag-bg-yellow', 'h-tag-bg-green', 'h-tag-bg-red']

const getDateTime = (timestamp) => {
	let now = new Date(timestamp * 1000)
	let year = now.getFullYear()
	let month = now.getMonth() + 1
	let date = now.getDate()
	let hour = now.getHours()
	let minute = now.getMinutes()
	let second = now.getSeconds()
	return year + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date) + ' ' + (hour < 10 ?
		'0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
}

const getArrayById = (arr, id, field = '') => {
	let data
	arr.forEach(v => {
		if (v.id == id) {
			data = v
		}
	})
	if (field && data) {
		return data[field]
	} else {
		return data
	}
}

const putCustomFormData = (form, data) => {
	let newForm = JSON.parse(JSON.stringify(form))
	if (!data) {
		return newForm
	}
	newForm.forEach(v => {
		if (data[v.field]) {
			v.value = data[v.field]
		}
	})
	return newForm
}

const getCustomFormData = (form) => {
	let json = {}
	form.forEach(v => {
		json[v.field] = v.value
	})
	return json
}

const getQueryString = (name) => {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
	let r = window.location.hash.substr(3).match(reg)
	if (r != null) {
		return (r[2])
	}
	return null
}

const inArray = (arr = [], key) => {
	let ret = false
	arr.forEach(v => {
		if (v == key) {
			ret = true
		}
	})
	return ret
}

const getArray = (arr = [], value, field, ret) => {
	let data
	arr.forEach((v, k) => {
		if (field) {
			if (v[field] == value) {
				if(ret == 'index'){
					data = k
				}else{
					data = v
				}
			}	
		} else {
			if (v == value) {
				if(ret == 'index'){
					data = k
				}else{
					data = v
				}
			}
		}
	})
	if (ret && ret != 'index') {
		data = data[ret]
	}
	return data
}

const getArrayName = (arr, id) => {
	let data
	arr.forEach(v => {
		if (Array.isArray(id)) {
			data = data || []
			if (inArray(id, v.id)) {
				data.push(v.name)
			}
		} else if (v.id == id) {
			data = v.name
		}
	})
	return data
}

const array2Tree = (arr) => {
	let newArr = JSON.parse(JSON.stringify(arr))
	if (!Array.isArray(newArr) || !newArr.length) return
	let map = {}
	newArr.forEach(item => map[item.id] = item)

	let roots = []
	newArr.forEach(item => {
		const parent = map[item.fid]
		if (parent) {
			(parent.children || (parent.children = [])).push(item)
		} else {
			roots.push(item)
		}
	})
	return roots
}

const http = (method, path, param, headers = {}) => {
	return new Promise((resolve, reject) => {
		axios({
			headers: headers,
			url: path,
			method: method,
			data: param,
		}).then(res => {
			resolve(res.data)
		}).catch((error) => {
			reject(error)
		})
	})
}

const imageToBase64 = (file) => {
	return new Promise((resolve, reject) => {
		let reader = new FileReader()
		reader.readAsDataURL(file)
		reader.onload = () => {
			resolve(reader.result)
		}
		reader.onerror = reject
	})
}

const categoryBug = (resolve,data) => {
	setTimeout(() => {resolve(data),1})
}

export default {
	config,
	getDateTime,
	tagColor,
	putCustomFormData,
	getCustomFormData,
	getQueryString,
	getArray,
	getArrayName,
	getArrayById,
	array2Tree,
	inArray,
	http,
	imageToBase64,
	categoryBug
}
